<?php
namespace app\modules\api\controllers;

use yii;
use yii\web\Controller;
use app\lib\BaseFunction;
use Lcobucci\JWT\Parser;
use \app\service\LoginService;

class BaseController extends Controller
{

    public $enableCsrfValidation = false;

    public $user;

    public function beforeAction($action)
    {
        $token = yii::$app->request->headers->get('authorization');
        if(!$token) {
            echo json_encode(['code'=>401,'msg'=>'未认证']);exit;
        }
        $baseUtils = new BaseFunction();
        $re = $baseUtils->verify_jwt($token);
        if (!$re) {
            echo  json_encode(['code'=>401,'msg'=>'未认证']);exit;
        }
        $token = (new Parser())->parse($token);
        $user_id = $token->getClaim('id');
        $loginService = new LoginService();

        $userInfo = $loginService->getUser($user_id);
        //当前用户对应的员工id
        $staffInfo = $loginService->getStaffInfo($userInfo['staff_id']);
        unset($staffInfo['id']);
        $staffInfo['zh_name'] = $staffInfo['name'];
        $this->user = array_merge($userInfo, $staffInfo);
        return parent::beforeAction($action);
    }
}